package com.css.dzj.zfjg.modules.complaintinfo.controller;


import com.alibaba.fastjson.JSON;
import com.css.dzj.zfjg.common.audit.service.SysAuditService;
import com.css.dzj.zfjg.common.util.BeanUtil;
import com.css.dzj.zfjg.common.util.Constant;
import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.modules.complaintinfo.entity.JgLhjgComplaintInfoEntity;
import com.css.dzj.zfjg.modules.complaintinfo.service.JgLhjgComplaintInfoService;
import com.querydsl.core.QueryResults;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;

/**
 * 企业投诉信息
 * @author wt
 * @date 2020\2\19 0019
 */
@RestController
@RequestMapping("generator/jglhjgcomplaintinfo")
public class JgLhjgComplaintInfoController {

    @Autowired
    private JgLhjgComplaintInfoService jgLhjgComplaintInfoService;

    // base日志接口
    @Autowired
    SysAuditService sysAuditService;
    @Autowired
    HttpServletRequest request;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Integer pageSize, @RequestParam Integer pageNo){
        String s = "";
        try{
            PageRequest pageRequest = PageRequest.of(pageNo - 1, pageSize);
            QueryResults<JgLhjgComplaintInfoEntity> comResults = jgLhjgComplaintInfoService.queryPage(pageRequest);
            s = JSON.toJSONString(comResults);
            sysAuditService.saveSuccessAudit("分页查询企业投诉信息成功", Constant.ZFJG_JGLHJGCOMPLAINTINFO, Constant.PT, Constant.ZFJG_PAGE, s, request);
            return R.ok().put("data", comResults);
        }catch (Exception e){
            e.printStackTrace();
            sysAuditService.saveFailAudit("分页查询企业投诉信息失败", Constant.ZFJG_JGLHJGCOMPLAINTINFO, Constant.YZ, Constant.ZFJG_PAGE, s, e, request);
            return R.error(e.getMessage());
        }

    }


    /**
     * 信息
     */
    @GetMapping("/info")
    public R info(@RequestParam String complaintInfoId){
        String s = "";
        try {
            JgLhjgComplaintInfoEntity jgLhjgComplaintInfo = jgLhjgComplaintInfoService.findById(complaintInfoId);
            s = JSON.toJSONString(jgLhjgComplaintInfo);
            sysAuditService.saveSuccessAudit("通过id获取企业投诉信息成功", Constant.ZFJG_JGLHJGCOMPLAINTINFO, Constant.PT, Constant.ZFJG_SELECT, s, request);
            return R.ok().put("jgLhjgComplaintInfo", jgLhjgComplaintInfo);
        } catch (Exception e) {
            e.printStackTrace();
            sysAuditService.saveFailAudit("通过id获取企业投诉信息失败", Constant.ZFJG_JGLHJGCOMPLAINTINFO, Constant.YZ, Constant.ZFJG_SELECT, s, e, request);
            return R.error(e.getMessage());
        }

    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public R save(@RequestBody JgLhjgComplaintInfoEntity jgLhjgComplaintInfo){
        String s  = "";
        try {
            if(StringUtils.isEmpty(jgLhjgComplaintInfo)) {
                return R.error("企业投诉信息不能为空");
            }
            jgLhjgComplaintInfoService.add(jgLhjgComplaintInfo);
            s  = JSON.toJSONString(jgLhjgComplaintInfo);
            sysAuditService.saveSuccessAudit("企业投诉信息添加成功", Constant.ZFJG_JGLHJGCOMPLAINTINFO, Constant.PT, Constant.ZFJG_ADD, s, request);
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(jgLhjgComplaintInfo);
            sysAuditService.saveFailAudit("企业投诉信息添加失败", Constant.ZFJG_JGLHJGCOMPLAINTINFO, Constant.YZ, Constant.ZFJG_ADD, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody JgLhjgComplaintInfoEntity jgLhjgComplaintInfo){
        String s = "";
        try {
            JgLhjgComplaintInfoEntity oldjgLhjgComplaintInfo = jgLhjgComplaintInfoService.findById(jgLhjgComplaintInfo.getComplaintInfoId());
            BeanUtil.copyBeanNotEmptyToBean(jgLhjgComplaintInfo, oldjgLhjgComplaintInfo);
            jgLhjgComplaintInfoService.update(jgLhjgComplaintInfo);
            s = JSON.toJSONString(jgLhjgComplaintInfo);
            sysAuditService.saveSuccessAudit("企业投诉信息修改成功", Constant.ZFJG_JGLHJGCOMPLAINTINFO, Constant.PT, Constant.ZFJG_UPDATE, s, request);
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(jgLhjgComplaintInfo);
            sysAuditService.saveFailAudit("企业投诉信息修改失败", Constant.ZFJG_JGLHJGCOMPLAINTINFO, Constant.YZ, Constant.ZFJG_UPDATE, s, e, request);
            return R.error(e.getMessage());
        }
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    public R delete(@RequestParam String complaintInfoId){
        String  s = "";
        try {
            JgLhjgComplaintInfoEntity oljgLhjgComplaintInfo = jgLhjgComplaintInfoService.findById(complaintInfoId);
            jgLhjgComplaintInfoService.delete(oljgLhjgComplaintInfo);
            s = JSON.toJSONString(complaintInfoId);
            sysAuditService.saveSuccessAudit("企业投诉信息删除成功", Constant.ZFJG_JGLHJGCOMPLAINTINFO, Constant.PT, Constant.ZFJG_DELETE, s, request);
            return R.ok();
        } catch(Exception e) {
            e.printStackTrace();
            s = JSON.toJSONString(complaintInfoId);
            sysAuditService.saveFailAudit("企业投诉信息删除失败", Constant.ZFJG_JGLHJGCOMPLAINTINFO, Constant.YZ, Constant.ZFJG_DELETE, s, e, request);
            return R.error(e.getMessage());
        }
    }

}
